/**
 * Created by Administrator on 2017/10/19
 */
import React from 'react';
import { observer } from 'mobx-react';
import WorkflowShare from './WorkflowShare';

import { WeaRightMenu, WeaLocaleProvider } from 'ecCom';
import { Modal } from 'antd';
import './style/share.css';
import * as lodash from 'lodash';

import WfAddShareStore from '../../stores/share/wfAddShareStore';
import WfAddShare from './AddShare';

const wfAddShareStore = new WfAddShareStore();

const getLabel = WeaLocaleProvider.getLabel;
let requestid, wfid, _this,f_weaver_belongto_userid,f_weaver_belongto_usertype;

@observer
export default class Main extends React.Component {
    constructor(props) {
        super(props);
        _this = this;
    }
    getFormParams(){
        const { location,formParams } = this.props;
        return location && !lodash.isEmpty(location.query) ? location.query : formParams ;
    }

    componentDidMount() {
        const {wfShareStore} = this.props;
        const { initDatas } = wfShareStore;
        const formParams = this.getFormParams();
        requestid = formParams.requestid;
        wfid = formParams.workflowid;
        f_weaver_belongto_userid = formParams.f_weaver_belongto_userid;
        f_weaver_belongto_usertype = formParams.f_weaver_belongto_usertype;
        let params = {
            requestid: requestid,
            wfid: wfid,
            f_weaver_belongto_userid: f_weaver_belongto_userid,
            f_weaver_belongto_usertype: f_weaver_belongto_usertype
        };
        initDatas(params);
    }

    getSelectedRowKeysTail() {
        const {wfShareStore} = this.props;
        const {tableStore} = wfShareStore;
        const tableNow = tableStore;
        const selectedRowKeys = tableNow.selectedRowKeys;
        const idsJson = JSON.stringify(selectedRowKeys);
        let ids = idsJson.slice(1, idsJson.length - 1).replace(/\"/g, "");
        return ids;
    }

    deleteConfirm(v) {
        const {wfShareStore} = this.props;
        const {deleteDatas} = wfShareStore;
        const ids = v;
        if (ids) {
            Modal.confirm({
                title: getLabel(15172,'系统提示'),
                content: getLabel(15097,'确定要删除吗？'),
                okText: getLabel(83446,'确定'),
                cancelText: getLabel(31129,'取消'),
                onOk: () => {
                    let params = {
                        requestid: requestid,
                        wfid: wfid,
                        ids: ids,
                        f_weaver_belongto_userid: f_weaver_belongto_userid,
                        f_weaver_belongto_usertype: f_weaver_belongto_usertype
                    };
                    deleteDatas(params);
                }
            });
        } else {
            Modal.warning({
                title: getLabel(15172,'系统提示'),
                content: getLabel(32568,'请选择要删除的记录'),
            });
        }
    }

    handleOk(v) {

        let {permissiontype,iscanread,objValue,seclevel,seclevelMax,rolelevel,joblevel,relatedshareid_6} = v;
        const {wfShareStore} = this.props;
        const {addDatas} = wfShareStore;

        let params = {};

        switch (Number(permissiontype)) {
            case 1 :
                params = {
                    departmentid: objValue,
                    seclevel: seclevel,
                    seclevelMax: seclevelMax,
                };
                break;
            case 6 :
                params = {
                    subids: objValue,
                    seclevel: seclevel,
                    seclevelMax: seclevelMax,
                };
                break;
            case 3 :
                params = {
                    seclevel: seclevel,
                    seclevelMax: seclevelMax,
                };
                break;
            case 5 :
                params = {
                    userid : objValue,
                };
                break;
            case 2 :
                params = {
                    roleid : objValue,
                    rolelevel : rolelevel,
                    seclevel: seclevel,
                    seclevelMax: seclevelMax,
                };
                break;
            case 7 :
                params = {
                    jobid : objValue,
                    joblevel: joblevel,
                    relatedshareid_6: relatedshareid_6
                }
        }

        params.requestid = requestid;
        params.wfid = wfid;
        params.permissiontype = permissiontype;
        params.iscanread = iscanread;
        const formParams = this.getFormParams();
        params.currentnodeid = formParams.currentnodeid;
        params.f_weaver_belongto_userid = f_weaver_belongto_userid;
        params.f_weaver_belongto_usertype = f_weaver_belongto_usertype;

        addDatas(params);
        this.handleCancel();
    }

    handleCancel() {
        wfAddShareStore.setState({visible:false})
    }

    addShare() {
        wfAddShareStore.setState({visible:true});
    }

    getRightMenu() {
        const {wfShareStore} = this.props;
        const {jurisdiction, tableStore} = wfShareStore;
        const ids = this.getSelectedRowKeysTail();
        let btns = [];
        if (jurisdiction) {
            btns.push(
                {
                    key: "search",
                    icon: <i className='icon-coms-New-Flow'/>,
                    content: getLabel(611,'添加'),
                    onClick: key => this.addShare()
                }, {
                    key: "deleteMore",
                    icon: <i className='icon-coms-Batch-delete'/>,
                    content: getLabel(32136,'批量删除'),
                    disabled: !ids,
                    onClick: key => this.deleteConfirm(ids)
                }
            );
        }
        btns.push(
            {
                key: "showCustomization",
                icon: <i className='icon-coms-Custom'/>,
                content: getLabel(32535,'显示列定制'),
                onClick: key => {
                    tableStore.setColSetVisible(true);
                    tableStore.tableColSet(true);
                }
            }
        );
        return btns;
    }

    render() {
        const {wfShareStore} = this.props;
        const {jurisdiction, tableStore} = wfShareStore;
        return (
            <div className="wf-workflowShareIndex">
                <WeaRightMenu ecId={`${this && this.props && this.props.ecId || ''}_WeaRightMenu@3r46s8`}
                    datas={this.getRightMenu()}
                    collectParams={{
                        favname: getLabel(2112,'共享设置'),
                        favouritetype: 2,
                        objid: 0,
                        importantlevel:1,
                    }}
                >

                    <WfAddShare ecId={`${this && this.props && this.props.ecId || ''}_WfAddShare@533i9j`}
                        wfAddShareStore={wfAddShareStore}
                        onOk={v => this.handleOk(v)}
                    />

                    <WorkflowShare ecId={`${this && this.props && this.props.ecId || ''}_WorkflowShare@8f4307`}
                        title={getLabel(2112,'共享设置')}
                        jurisdiction={jurisdiction}
                        tableStore={tableStore}
                        deleteConfirm={(v) => this.deleteConfirm(v)}
                        addShare={() => this.addShare()}
                        ids={() => this.getSelectedRowKeysTail()}
                    />
                </WeaRightMenu>
            </div>
        )
    }
}

window.workflowShareOperate = (function () {
    return {
        onDel(ids) {
            _this.deleteConfirm(ids);
            return;
        }
    }
})();
